<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 发现分类
 * Class Category
 * @author  heyu
 */
class Category extends MY_Controller {

    /**
     * @var ConsumerFindCategoryModel
     */
    public $mFindCategory;
    /**
     * @var ConsumerFindAttributeModel
     */
    public $mAttribute;

	public function __construct(){
		parent::__construct();
        $this->loadModel('ConsumerFindCategoryModel', 'mFindCategory', 'consumer');
        $this->loadModel('ConsumerFindAttributeModel', 'mAttribute', 'consumer');
	}
	
	/**
	 * 首页
	 */
	public function index(){
        $attrCateId = $this->mAttribute->adminPcGetCategoryList();
        $data = array(
            'attrCateId' => $attrCateId,
            'title' => '发现分类'
        );
		$this->view('find/category', $data, 'validation,nestable,datepicker,cropit_master');
	}

    /**
     * 获取数据
     */
    public function getList(){
        $data = $this->mFindCategory->adminPcGetList();
        echo json_encode($data);
    }

    /**
     * 获取分类详情
     */
    public function getInfo(){
        $id = (int)post('id');
        if (!$id){
            $this->ajaxReturn(0, '数据错误');
        }
        $info = $this->mFindCategory->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '分类不存在');
        }
        $this->ajaxReturn(1, '', $info);
    }

    /**
     * 保存
     */
    public function save(){
        //表单验证
        $vConf = array(
            array('name', '分类名称', 'required|max_length[4]'),
            array('sort', '排序', 'is_natural_no_zero'),
            array('attr_cate_id', '特殊属性分类id', 'required|is_numeric'),
        );
        $this->formDataValidation($vConf);
        $sort = (int)post('sort');
        $id = (int)post('id');
        $attr_cate_id = (int)post('attr_cate_id');
        $portrait = post('portrait');

        // 验证分类
        if($attr_cate_id){
            $category = $this->mAttribute->getCategoryInfo($attr_cate_id);
            if (!$category){
                $this->ajaxReturn(0, '特殊属性分类不存在');
            }
        }

        $data = array('name' => post('name'), 'attr_cate_id' => $attr_cate_id);
        if ($id){
            $info = $this->mFindCategory->getInfo($id);
            if (!$info){
                $this->ajaxReturn(0, '分类不存在');
            }
            if ($sort){
                $data['sort'] = $sort;
            }
            $tA = $this->mFindCategory->save($data, $id);
            if (!$tA){
                $this->ajaxReturn(0, '保存失败');
            }
        }else{
            $data['sort'] = $sort ? $sort : $this->mFindCategory->getMaxSort();
            $data['addtime'] = time();
            $id = $this->mFindCategory->save($data);
            if (!$id){
                $this->ajaxReturn(0, '保存失败');
            }
        }
        /**
         *  上传图标
         */
        if($portrait){
            $path = 'uploads/find/category/'.$id.'/';
            $ts = saveCSVimg(array($portrait), $path, $id,true);
            if($ts !== false){
                $tId = $this->mFindCategory->save(array('icon'=>$ts[0]), $id);
                if( ! $tId){
                    $this->appReturn(0, '保存失败');
                }
            }else{
                $this->ajaxReturn(0, '图标上传失败');
            }
        }
        $this->ajaxReturn(1, '保存成功');
    }

    /**
     * 分类排序
     */
    public function modelSort(){
        $data = post('data');
        if (empty($data)){
            $this->ajaxReturn(0, '数据错误');
        }
        if(is_array($data)) {
            $count = count($data);
            foreach ($data as $k => $v) {
                $dbData[] = array('id' => $v['id'], 'sort' => $count);
                $count--;
            }
            if ($dbData) {
                $this->mFindCategory->saveBatch($dbData, 'id');
            }
        }
        $this->ajaxReturn(1, '保存成功');
    }

    /**
     * 删除
     */
    public function delete(){
        //表单验证
        $vConf = array(
            array('id', 'id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
        $id = (int)get('id');
        $info = $this->mFindCategory->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '分类不存在');
        }

        $this->loadModel('ConsumerFindModel', 'mFind', 'consumer');

        $data = $this->mFind->getFindByNewsCategory($id);
        if (!empty($data)){
            $this->ajaxReturn(0, '该分类下有发现信息， 请先删除发现信息！');
        }
        $tA = $this->mFindCategory->deleteNewsType($id);
        if (!$tA){
            $this->ajaxReturn(0, '删除失败');
        }
        $this->ajaxReturn(1, '删除成功');
    }

}
